/*
 * Base.java
 * 
 * Copyright (c) 2007 Aris Bartee
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * Created on Jun 4, 2007, 11:55:29 PM
 * 
 */
package afterburner.jsp;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.HttpJspPage;

/**
 *
 * @author arisbartee
 */
public abstract class Base implements HttpJspPage {

    public final static String FLASH = "afterburner.flash";
    protected ServletConfig config = null;
    protected HttpServletRequest _request = null;
    protected HttpServletResponse _response = null;
    protected String layoutPath = null;
    protected Map<String, String> outputMap = null;
    private boolean postMethod = false;

    public Base() {
    }

    /**
     * @see javax.servlet.jsp.HttpJspPage#jspInit
     */
    public void jspInit() {

    }

    /**
     * @see javax.servlet.jsp.HttpJspPage#jspDestroy
     */
    public void jspDestroy() {
    }

    /**
     * @see javax.servlet.jsp.HttpJspPage#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        this.config = config;
        outputMap = new HashMap<String, String>();
    }

    /**
     * @see javax.servlet.jsp.HttpJspPage#destroy
     */
    public void destroy() {
        config = null;
        _request = null;
        _response = null;
        layoutPath = null;
        outputMap = null;
    }

    /**
     * @see javax.servlet.jsp.HttpJspPage#getServletConfig
     */
    public ServletConfig getServletConfig() {
        return config;
    }

    /**
     * @see javax.servlet.jsp.HttpJspPage#getServletInfo
     */
    public String getServletInfo() {
        return "This JSP is using Afterburner.";
    }

    /**
     * @see javax.servlet.jsp.HttpJspPage#_jspService
     */
    public void _jspService(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public boolean isPost(){
        return postMethod;
    }
    
    public boolean isGet(){
        return !postMethod;
    }

    private void setRequest(HttpServletRequest request) {
        this._request = request;
        if(request != null){
            postMethod = request.getMethod().equalsIgnoreCase("POST");
        }
    }

    private void setResponse(HttpServletResponse response) {
        this._response = response;
    }

    /**
     * Convenience method for retrieving the HttpServletRequest
     */
    public HttpServletRequest getRequest() {
        return _request;
    }

    /**
     * Convenience method for retrieving the HttpServletResponse
     */
    public HttpServletResponse getResponse() {
        return _response;
    }

    protected void setRequestResponse(ServletRequest srequest, ServletResponse sresponse) {
        setRequest((HttpServletRequest) srequest);
        setResponse((HttpServletResponse) sresponse);
    }

    public void setFlash(String name, String value) {
        _request.getSession().setAttribute(FLASH + "." + name, value);
    }

    public String getFlash(String name) {
        String value = (String) _request.getSession().getAttribute(FLASH + "." + name);
        _request.getSession().removeAttribute(FLASH + "." + name);
        return value;
    }

    public String param(String name) {
        String value = _request.getParameter(name);
        if (value != null && value.trim().length() == 0) {
            value = null;
        }
        return value;
    }

    public String[] params(String name) {
        String value[] = _request.getParameterValues(name);
        if (value == null) {
            value = new String[0];
        }
        return value;
    }
    
    public void log(String message){
        config.getServletContext().log(message);
    }
}
